﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace VOAEnglishDownload
{
    public partial class VOAEnlishDownloader : Form
    {
        delegate void SetTextCallback(string text, int current, int max);

        private SharpDownloadSDK.HttpWebRequestDownloader downloader = new SharpDownloadSDK.HttpWebRequestDownloader();
        private VOAEnglishDownload.VOASpecialEnglish voaUrls = new VOAEnglishDownload.VOASpecialEnglish();       

        private int year = 0;
        private String savePath = String.Empty;

        public VOAEnlishDownloader()
        {
            InitializeComponent();
            InitializeControls();
            InitializeProgressBar();

            downloader.DownloadFileCompletedEvent += new SharpDownloadSDK.DownloadFileCompleted(DownLoadFileCompletedEventHandler);
            voaUrls.AnalyseFileCompletedEvent1 += new VOAEnglishDownload.VOASpecialEnglish.AnalyseFileCompletedEvent(AnalyseFileCompletedEventHandler);
        }

        #region Event hanlders
        private void savePathButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                savePathTextBox.Text = dialog.SelectedPath;
            }
        }

        private void VOAEnlishDownloader_Load(object sender, EventArgs e)
        {
        }

        private void DownloadButton_Click(object sender, EventArgs e)
        {
            year = (int)(this.voaYearComboBox.SelectedItem);
            savePath = this.savePathTextBox.Text + "\\";


            CurrentLabel.Text = "开始分析和下载！";
            UsingBackGroundThread();
        }

        private void AnalyseCompletedPercentEventHandler(float percent)
        {

        }
        private void AnalyseFileCompletedEventHandler(String url, String fullPath)
        {
            if (!downloader.UrlAndSavePaths.ContainsKey(url))
            {
                downloader.UrlAndSavePaths.Add(url, fullPath);
                downloader.DownloadFileAsync(url, fullPath);
            }
        }

        private void DownLoadFileCompletedEventHandler(Object sender, SharpDownloadSDK.DownloadFileCompletedEventArgs e)
        {
            String result = String.Empty;
            String currentFileNum = "(" + downloader.CurrentCompletedFileNum.ToString() + "/" + downloader.UrlAndSavePaths.Count.ToString() + ")";
            if (downloader.CurrentCompletedFileNum == downloader.UrlAndSavePaths.Count)
            {
                result = currentFileNum + "下载可能完成！";
            }

            if (e.IsSuccessful)
            {
                result = currentFileNum + "下载成功：" + e.SavePath;
            }
            else
            {
                result = currentFileNum + "下载失败：" + e.SavePath;                
            }

            SetLabelAndProgressBar(result,(int)downloader.CurrentCompletedFileNum, downloader.UrlAndSavePaths.Count);
        }
        #endregion

        #region Private functions
        private void InitializeControls()
        { 
            this.voaYearComboBox.Items.Add(2007);
            this.voaYearComboBox.Items.Add(2006);
            this.voaYearComboBox.Items.Add(2005);
            this.voaYearComboBox.Items.Add(2004);
            this.voaYearComboBox.Items.Add(2003);
            this.voaYearComboBox.Items.Add(2002);
            this.voaYearComboBox.Items.Add(2001);

            this.voaYearComboBox.SelectedIndex = 0;
        }
        private void InitializeProgressBar()
        {
            this.progressBar1.Minimum = 0;
            this.progressBar1.Maximum = 100;
            this.progressBar1.Style = ProgressBarStyle.Continuous;
            this.progressBar1.RightToLeft = RightToLeft.Yes;
            this.progressBar1.ForeColor = Color.Azure;
        }

        private void SetLabelAndProgressBar(string text,int current, int max)
        {
            if (this.CurrentLabel.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetLabelAndProgressBar);
                this.Invoke(d, new object[] { text, current, max});
            }
            else
            {
                this.CurrentLabel.Text = text;
                this.progressBar1.Maximum = max;
                this.progressBar1.Value = current;
            }
        }
        private void AnalyseAllMp3Urls()
        {
            voaUrls.SaveDirectory = savePath;
            voaUrls.VOAYear = year;
            voaUrls.GetUrls();

            this.CurrentLabel.Text = "分析完成！";
        }
        private void UsingBackGroundThread()
        {
            Thread t = new Thread(DoAll);
            t.IsBackground = true;
            t.Start();
        }

        private void DoAll()
        {
            AnalyseAllMp3Urls();
        }

        #endregion      
    }
}
